#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

// 抽象基类Shape，定义计算面积的虚函数
class Shape {
public:
    virtual double calculateArea() = 0;
};

// Circle类，继承自Shape类，用于表示圆形
class Circle : public Shape {
private:
    double radius;
public:
    Circle(double r) : radius(r) {}
    double calculateArea() override {
        return 3.1415926 * radius * radius;  // 圆的面积公式：π * r²
    }
};

// Rectangle类，继承自Shape类，用于表示矩形
class Rectangle : public Shape {
private:
    double length;
    double width;
public:
    Rectangle(double l, double w) : length(l), width(w) {}
    double calculateArea() override {
        return length * width;  // 矩形的面积公式：长 * 宽
    }
};

int main() {
    srand(static_cast<unsigned int>(time(0)));
    const int numShapes = 10;
    Shape* shapes[numShapes];

    // 随机创建不同类型的形状对象并添加到数组中
    for (int i = 0; i < numShapes; ++i) {
        int randomType = rand() % 2;
        if (randomType == 0) {
            double radius = static_cast<double>(rand() % 10 + 1);  // 随机生成半径范围1-10
            shapes[i] = new Circle(radius);
        } else {
            double length = static_cast<double>(rand() % 10 + 1);  // 随机生成长度范围1-10
            double width = static_cast<double>(rand() % 10 + 1);  // 随机生成宽度范围1-10
            shapes[i] = new Rectangle(length, width);
        }
    }

    // 循环计算并输出每个形状的面积
    for (int i = 0; i < numShapes; ++i) {
        cout << "Shape " << (i + 1) << " area: " << shapes[i]->calculateArea() << endl;
        delete shapes[i];
    }

    return 0;
}
